/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.cards;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.demo.drawable.CircleDrawable;
import it.gmariotti.cardslib.demo.drawable.RoundCornersDrawable;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardExpand;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.internal.CardThumbnail;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.media.image.PixelMap;

/**
 * This class provides a simple card as Google Now Birthday
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class GoogleNowBirthCard extends Card {

    public int USE_VIGNETTE = 0;

    public GoogleNowBirthCard(Context context) {
        super(context, ResourceTable.Layout_carddemo_googlenowbirth_inner_main);
        init();
    }

    public GoogleNowBirthCard(Context context, int innerLayout) {
        super(context, innerLayout);
        init();
    }

    private void init() {

        //Add Header
        GoogleNowBirthHeader header = new GoogleNowBirthHeader(getContext(), ResourceTable.Layout_carddemo_googlenowbirth_inner_header);
        header.setButtonExpandVisible(true);
        header.mName = "Gabriele Mariotti";
        header.mSubName = "Birthday today";
        addCardHeader(header);

        //Add Expand Area
        CardExpand expand = new GoogleNowExpandCard(getContext());
        addCardExpand(expand);

        //Set clickListener
        setClickedListener((card, view) -> new ToastDialog(getContext()).setText("Click Listener card").show());

        //Add Thumbnail
        GoogleNowBirthThumb thumbnail = new GoogleNowBirthThumb(getContext());
        float density = getContext().getResourceManager().getDeviceCapability().screenDensity;
        int size = (int) (125 * density);
        thumbnail.setUrlResource("https://lh5.googleusercontent.com/-squZd7FxR8Q/UyN5UrsfkqI/AAAAAAAAbAo/VoDHSYAhC_E/s" + size + "/new%2520profile%2520%25282%2529.jpg");
        thumbnail.setErrorResource(ResourceTable.Media_ic_ic_error_loading);
        addCardThumbnail(thumbnail);
    }

    @Override
    public void setupInnerViewElements(ComponentContainer parent, Component view) {

        Text title = (Text) view.findComponentById(ResourceTable.Id_card_main_inner_simple_title);
        title.setText("Wish Happy Birthday");
        try {
            title.setTextColor(new Color(mContext.getResourceManager().getElement(ResourceTable.Color_carddemo_googlenowbirth_title).getColor()));
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        title.setTextAlignment(TextAlignment.VERTICAL_CENTER);
    }


    class GoogleNowBirthThumb extends CardThumbnail {

        public GoogleNowBirthThumb(Context context) {
            super(context);
        }

        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component viewImage) {
            /*
            viewImage.getLayoutConfig().width = 250;
            viewImage.getLayoutConfig().height = 250;
            */
        }

        @Override
        public boolean applyBitmap(Component imageView, PixelMap bitmap) {
            switch (USE_VIGNETTE) {
                case 0:
                    return false;
                case 1:
                    //RounderImage
                    int CORNER_RADIUS = 12; // dips
                    //int MARGIN = 12; // dips

                    float density = getContext().getResourceManager().getDeviceCapability().screenDensity;
                    int mCornerRadius = (int) (CORNER_RADIUS * density + 0.5f);
                    int mMargin = 0;
                    RoundCornersDrawable round = new RoundCornersDrawable(bitmap, mCornerRadius, mMargin);
                    ((Image)imageView).setImageElement(round);
                    return true;
                case 2:
                    CircleDrawable circle = new CircleDrawable(bitmap, true);
                    ((Image)imageView).setImageElement(circle);
                    return true;
                default:
                    return false;
            }
        }
    }


    class GoogleNowBirthHeader extends CardHeader {

        String mName;
        String mSubName;

        public GoogleNowBirthHeader(Context context, int innerLayout) {
            super(context, innerLayout);
        }

        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component view) {

            Text txName = (Text) view.findComponentById(ResourceTable.Id_text_birth1);
            Text txSubName = (Text) view.findComponentById(ResourceTable.Id_text_birth2);

            txName.setText(mName);
            txSubName.setText(mSubName);
        }
    }
}
